package com.lz.gmall.product.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.lz.gmall.product.dto.SpuInfoDto;
import com.lz.gmall.product.entity.*;
import com.lz.gmall.product.service.SpuImageService;
import com.lz.gmall.product.service.SpuInfoService;
import com.lz.gmall.product.mapper.SpuInfoMapper;
import com.lz.gmall.product.service.SpuSaleAttrService;
import com.lz.gmall.product.service.SpuSaleAttrValueService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.stream.Collectors;

/**
 *
 */
@Service
public class SpuInfoServiceImpl extends ServiceImpl<SpuInfoMapper, SpuInfo>
    implements SpuInfoService{

    @Autowired
    private SpuImageService spuImageService;

    @Autowired
    private SpuSaleAttrService spuSaleAttrService;

    @Autowired
    private SpuSaleAttrValueService spuSaleAttrValueService;

    @Override
    public Page<SpuInfo> getSpuInfoPage(Integer pageNum, Integer limit, Long c3Id) {
        Page<SpuInfo> page = new Page<>(pageNum, limit);
        LambdaQueryWrapper<SpuInfo> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(SpuInfo::getCategory3Id,c3Id);
        this.page(page,wrapper);
        return page;
    }

    @Override
    @Transactional
    public void saveSpuInfo(SpuInfoDto spuInfoDto) {
        //保存spu基本数据到spu_info
        SpuInfo spuInfo = new SpuInfo();
        BeanUtils.copyProperties(spuInfoDto,spuInfo);
        save(spuInfo);

        //保存图片数据到spu_image
        List<SpuImage> spuImageList = spuInfoDto.getSpuImageList();
        spuImageList = spuImageList.stream().map(spuImage -> {
            spuImage.setSpuId(spuInfo.getId());
            return spuImage;
        }).collect(Collectors.toList());
        spuImageService.saveBatch(spuImageList);

        //添加spu销售属性和销售属性值
        List<SpuSaleAttr> spuSaleAttrList = spuInfoDto.getSpuSaleAttrList();
        spuSaleAttrList.stream().forEach(spuSaleAttr -> {
            //保存销售属性名数据到spu_sale_attr
            spuSaleAttr.setSpuId(spuInfo.getId());
            spuSaleAttrService.save(spuSaleAttr);

            //保存销售属性值数据到spu_sale_attr_value
            List<SpuSaleAttrValue> spuSaleAttrValueList = spuSaleAttr.getSpuSaleAttrValueList();
            spuSaleAttrValueList = spuSaleAttrValueList.stream().map(spuSaleAttrValue -> {
                //前端没有传回这两个参数，自己加上
                spuSaleAttrValue.setSpuId(spuInfo.getId());
                spuSaleAttrValue.setSaleAttrName(spuSaleAttr.getSaleAttrName());
                return spuSaleAttrValue;
            }).collect(Collectors.toList());
            spuSaleAttrValueService.saveBatch(spuSaleAttrValueList);
        });
    }
}




